﻿@inherits PmComponentBase

<div class="full-height project-list scrollbar-base scrollbar-8 rounded-5" style="overflow: auto;">
    @if (_projects.Any())
    {
        <MExpansionPanels Flat Accordion @key="@($"{EnvironmentClusterId}{TeamId}")">
            @foreach (var project in _projects)
            {
                var apps = _apps.Where(app => app.ProjectId == project.Id).OrderByDescending(app => app.ModificationTime).ToList();
                if (CanAddApp)
                {
                    apps = apps.Where(app => app.EnvironmentClusters.Select(ec => ec.Id).Contains(EnvironmentClusterId)).ToList();
                }
                <MExpansionPanel Class="rounded-5 my-3">
                    <MExpansionPanelHeader Style="height: 56px;" Class="my-6 project-expansion-panel-header">
                        <ChildContent>
                            <span style="display:flex;align-items:center;margin-right:10px;">
                                <div class="emphasis--text h6">
                                    @if (OnNameClick.HasDelegate)
                                    {
                                        <MHover Context="hoverContext">
                                            <span @onclick:stopPropagation @onclick="()=>HandleProjectNameClick(project.Id)" @attributes="hoverContext.Attrs" class="@(hoverContext.Hover ? "primary--text":"")">
                                                @project.Name
                                            </span>
                                        </MHover>
                                    }
                                    else
                                    {
                                        @project.Name
                                    }
                                    <div class="mt-1 regul ar3--text btn d-flex" style="padding:0">
                                        @if (!string.IsNullOrEmpty(project.LabelName))
                                        {
                                            string backgroundColor;
                                            switch (project.LabelCode.ToLower())
                                            {
                                                case "basicability":
                                                    backgroundColor = "#37D7AD";
                                                    break;
                                                case "operator":
                                                    backgroundColor = "#FFB547";
                                                    break;
                                                case "datafactory":
                                                    backgroundColor = "#5FB9FF";
                                                    break;
                                                default:
                                                    backgroundColor = "#A3AED0";
                                                    break;
                                            }
                                            <div class="d-flex justify-center mr-2" style="border-radius:6px; color:#fff; background-color:@backgroundColor; padding:0 4px; height: fit-content;">
                                                @project.LabelName
                                            </div>
                                        }
                                        <span class="regular3--text body2">@project.Identity</span>
                                        <span class="ml-2 regular3--text body2">|</span>
                                        <sapn class="ml-2 regular3--text body2">@T("AppCount"): @apps.Count</sapn>
                                    </div>
                                </div>
                                <MSpacer />
                                <div style="display:flex;align-items:center;">
                                    <SButton Icon OnClickStopPropagation Class="mr-1" OnClick="()=>UpdateProjectAsync(project.Id)"><SIcon Size="20" Color="#485585">mdi-pencil</SIcon></SButton>
                                    <MDivider Vertical Style="height: 20px;margin: 7px 8px 0 8px;" />
                                </div>
                            </span>
                        </ChildContent>
                        <ActionsContent>
                            <SIcon Color="#485585" IsDefaultToolTip="false">
                                mdi-chevron-down
                            </SIcon>
                        </ActionsContent>
                    </MExpansionPanelHeader>
                    <MExpansionPanelContent>
                        <MRow>
                            @{
                                if (CanAddApp)
                                {
                                    foreach (var app in apps)
                                    {
                                        <MCol Lg="4" Md="6" Sm="12" Class="m-card-wrap">
                                            <AppCard OnClick="()=> UpdateAppAsync(app)" App="app" EnvironmentClusters="app.EnvironmentClusters"></AppCard>
                                        </MCol>
                                    }
                                    <MCol Lg="4" Md="6" Sm="12" Class="m-card-wrap">
                                        <MRow NoGutters>
                                            <MCol>
                                                <SButton Color="" BorderRadiusClass="rounded-3" Height="162" OnClick="()=>ShowAppModalAsync(project.Id)" Tile Text Style="width: 100%; display: flex; justify-content: center; align-items: center; border: 2px dashed #E9EDF7;">
                                                    <SIcon Left IsDefaultToolTip="false">mdi-plus</SIcon><span class="emphasis--text">@T("Add App")</span>
                                                </SButton>
                                            </MCol>
                                        </MRow>
                                    </MCol>
                                }
                                else
                                {
                                    foreach (var app in apps.Take(3))
                                    {
                                        <MCol Lg="4" Md="6" Sm="12" Class="m-card-wrap">
                                            <AppCard OnClick="()=> UpdateAppAsync(app)" App="app" EnvironmentClusters="app.EnvironmentClusters"></AppCard>
                                        </MCol>
                                    }
                                }
                            }
                        </MRow>
                        <div class="d-flex justify-space-between mt-3">
                            <div>
                                @{
                                    var teamId = project.EnvironmentProjectTeams.FirstOrDefault(c => c.EnvironmentName == Environment && c.ProjectId == project.Id)?.TeamId;
                                    var team = _allTeams.FirstOrDefault(team => team.Id == teamId);
                                    if (team != null)
                                    {
                                        <MTooltip Right>
                                            <ActivatorContent>
                                                <MAvatar Size="24" @attributes="@context.Attrs">
                                                    <MImage Src="@team.Avatar"></MImage>
                                                </MAvatar>
                                            </ActivatorContent>
                                            <ChildContent>
                                                <span>@team.Name</span>
                                            </ChildContent>
                                        </MTooltip>
                                    }
                                }
                            </div>
                            @if (CanAddApp)
                            {
                                <div class="regular3--text btn">@project.ModifierName @T("Edit in") @project.ModificationTime.Humanize(culture:I18n.Culture)</div>
                            }
                            else
                            {
                                <MHover Context="hoverContext">
                                    @{
                                        var mClass = $"{(hoverContext.Hover ? "primary--text" : "regular2--text")}";
                                        <div @onclick="()=>HandleProjectNameClick(project.Id)" @attributes="hoverContext.Attrs" class="@($"hover-pointer btn {mClass}")">
                                            @T("More")
                                            <SIcon Class="@($"ml-1 {mClass}")" Size="18">mdi-chevron-right</SIcon>
                                        </div>
                                    }
                                </MHover>
                            }
                        </div>
                    </MExpansionPanelContent>
                </MExpansionPanel>
            }
        </MExpansionPanels>
    }
    else
    {
        <SEmptyPlaceholder />
    }
</div>
<MOverlay Class="rounded-5" Value="_showProcess" Absolute Color="#fdfdfd" Opacity="1">
    <MProgressCircular Width="4" Indeterminate Size="48" Color="primary"></MProgressCircular>
</MOverlay>

<ProjectModal EnvironmentClusterId="_internalEnvironmentClusterId"
              Environment="@Environment"
              TeamId="_internalTeamId"
              @ref="@_projectModal"
              Projects="_projects"
              DisableTeamSelect="@(_internalTeamId != Guid.Empty)"
              OnSubmitProjectAfter="OnSubmitProjectAsyncAfter" />
<AppModal @ref="@_appModal" OnSubmitProjectAfter="OnSubmitAppAsyncAfter" />
